package com.mzba.easy.weibo.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * @author 06peng
 *
 */
public class FavoriteTimelineTable {

	public String TABLE_NAME = "favorite_db";
	public static final String _id = "_id";
	public static final String idstr = "idstr";
	public static final String created_at = "created_at";
	public static final String id = "id";
	public static final String text = "text";
	public static final String source = "source";
	public static final String favorited = "favorited";
	public static final String truncated = "truncated";
	public static final String in_reply_to_status_id = "in_reply_to_status_id";
	public static final String in_reply_to_user_id = "in_reply_to_user_id";
	public static final String in_reply_to_screen_name = "in_reply_to_screen_name";
	public static final String mid = "mid";
	public static final String bmiddle_pic = "bmiddle_pic";
	public static final String original_pic = "original_pic";
	public static final String thumbnail_pic = "thumbnail_pic";
	public static final String reposts_count = "reposts_count";
	public static final String comments_count = "comments_count";
	public static final String user_id = "user_id";
	public static final String retweet_status_id = "retweet_status_id";
	public static final String screen_name = "screen_name";
	public static final String profile_image_url = "profile_image_url";
	
	public static final String retweet_status_text = "retweet_status_text";
	public static final String retweet_status_pic = "retweet_status_pic";
	public static final String retweet_reposts_count = "retweet_reposts_count";
	public static final String retweet_comments_count = "retweet_comments_count";
	public static final String retweet_source = "retweet_source";
	public static final String retweet_screen_name = "retweet_screen_name";
	public static final String retweet_created_at = "retweet_created_at";
	public static final String retweet_original_pic = "retweet_original_pic";
	public static final String retweet_bmiddle_pic = "retweet_bmiddle_pic";

	
	private DBManagerImpl db = null;
	
	public FavoriteTimelineTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	public void createTable() {
		String createTableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(_id integer primary key autoincrement, " 
				+ idstr + " VARCHAR," 
				+ created_at + "  VARCHAR," 
				+ id + " VARCHAR,"
				+ text + " VARCHAR,"
				+ source + " VARCHAR," 
				+ favorited + " Integer," 
				+ truncated + " Integer," 
				+ in_reply_to_status_id + " VARCHAR," 
				+ in_reply_to_user_id + " VARCHAR,"
				+ in_reply_to_screen_name + " VARCHAR," 
				+ mid + " VARCHAR,"
				+ bmiddle_pic + " VARCHAR," 
				+ original_pic + " VARCHAR,"
				+ thumbnail_pic + " VARCHAR," 
				+ reposts_count + " Integer,"
				+ comments_count + " Integer," 
				+ user_id + " VARCHAR, "
				+ retweet_status_id + " VARCHAR," 
				+ screen_name + " VARCHAR, "
				+ profile_image_url + " VARCHAR," 
				+ retweet_status_text + " VARCHAR,"
				+ retweet_status_pic + " VARCHAR,"
				+ retweet_reposts_count + " Integer,"
				+ retweet_source + " VARCHAR,"
				+ retweet_screen_name + " VARCHAR,"
				+ retweet_created_at + " VARCHAR,"
				+ retweet_original_pic + " VARCHAR,"
				+ retweet_bmiddle_pic + " VARCHAR,"
				+ retweet_comments_count + " Integer)";
		db.creatTable(db.getConnection(), createTableSql, TABLE_NAME);
		
	}
	
	public void save(List<StatusTimelineEntry> entries) {
		try {
			List<ContentValues> arraylist = new ArrayList<ContentValues>();
			db.delete(db.getConnection(), TABLE_NAME, null, null);
			for (int i = 0;i < 20;i++) {
				StatusTimelineEntry entry = entries.get(i);
				ContentValues values = new ContentValues();
				values.put(idstr, entry.getIdstr());
				values.put(created_at, entry.getCreated_at());
				values.put(id, entry.getId());
				values.put(text, entry.getText());
				values.put(source, entry.getSource());
				values.put(favorited, entry.isFavorited() ? 1 : 0);
				values.put(truncated, entry.isTruncated() ? 1: 0);
				values.put(in_reply_to_status_id, entry.getIn_reply_to_status_id());
				values.put(in_reply_to_user_id, entry.getIn_reply_to_user_id());
				values.put(in_reply_to_screen_name, entry.getIn_reply_to_screen_name());
				values.put(mid, entry.getMid());
				values.put(bmiddle_pic, entry.getBmiddle_pic());
				values.put(original_pic, entry.getOriginal_pic());
				values.put(thumbnail_pic, entry.getThumbnail_pic());
				values.put(reposts_count, entry.getReposts_count());
				values.put(comments_count, entry.getComments_count());
				values.put(user_id, entry.getUser().getId());
				values.put(screen_name, entry.getUser().getScreen_name());
				values.put(profile_image_url, entry.getUser().getProfile_image_url());
				if (entry.getRetweeted_status() != null) {
					values.put(retweet_status_id, entry.getRetweeted_status().getId());
					values.put(retweet_status_text, entry.getRetweeted_status().getText());
					values.put(retweet_status_pic, entry.getRetweeted_status().getThumbnail_pic());
					values.put(retweet_reposts_count, entry.getRetweeted_status().getReposts_count());
					values.put(retweet_comments_count, entry.getRetweeted_status().getComments_count());
					values.put(retweet_source, entry.getRetweeted_status().getSource());
					values.put(retweet_screen_name, entry.getRetweeted_status().getUser().getScreen_name());
					values.put(retweet_created_at, entry.getRetweeted_status().getCreated_at());
					values.put(retweet_bmiddle_pic, entry.getRetweeted_status().getBmiddle_pic());
					values.put(retweet_original_pic, entry.getRetweeted_status().getOriginal_pic());
				}
				arraylist.add(values);
			}
			db.saveAll(db.getConnection(), TABLE_NAME, arraylist);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<StatusTimelineEntry> getAllStatus() {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME, null);
		if (cursor == null)
			return null;
		List<StatusTimelineEntry> entries = new ArrayList<StatusTimelineEntry>();
		try {
			while (cursor.moveToNext()) {
				StatusTimelineEntry entry = new StatusTimelineEntry();
				entry.setIdstr(cursor.getString(cursor.getColumnIndex(idstr)));
				entry.setCreated_at(cursor.getString(cursor.getColumnIndex(created_at)));
				entry.setId(cursor.getString(cursor.getColumnIndex(id)));
				entry.setText(cursor.getString(cursor.getColumnIndex(text)));
				entry.setSource(cursor.getString(cursor.getColumnIndex(source)));
				entry.setFavorited(cursor.getInt(cursor.getColumnIndex(favorited)) == 1 ? true : false);
				entry.setTruncated(cursor.getInt(cursor.getColumnIndex(truncated)) == 1 ? true : false);
				entry.setIn_reply_to_status_id(cursor.getString(cursor.getColumnIndex(in_reply_to_status_id)));
				entry.setIn_reply_to_user_id(cursor.getString(cursor.getColumnIndex(in_reply_to_user_id)));
				entry.setIn_reply_to_screen_name(cursor.getString(cursor.getColumnIndex(in_reply_to_screen_name)));
				entry.setMid(cursor.getString(cursor.getColumnIndex(mid)));
				entry.setBmiddle_pic(cursor.getString(cursor.getColumnIndex(bmiddle_pic)));
				entry.setOriginal_pic(cursor.getString(cursor.getColumnIndex(original_pic)));
				entry.setThumbnail_pic(cursor.getString(cursor.getColumnIndex(thumbnail_pic)));
				entry.setReposts_count(cursor.getInt(cursor.getColumnIndex(reposts_count)));
				entry.setComments_count(cursor.getInt(cursor.getColumnIndex(comments_count)));
				UserEntry user = new UserEntry();
				user.setId(cursor.getString(cursor.getColumnIndex(user_id)));
				user.setScreen_name(cursor.getString(cursor.getColumnIndex(screen_name)));
				user.setProfile_image_url(cursor.getString(cursor.getColumnIndex(profile_image_url)));
				entry.setUser(user);
				if (cursor.getString(cursor.getColumnIndex(retweet_status_id)) != null && 
						!cursor.getString(cursor.getColumnIndex(retweet_status_id)).equals("0")) {
					StatusTimelineEntry retweetStatus = new StatusTimelineEntry();
					retweetStatus.setId(cursor.getString(cursor.getColumnIndex(retweet_status_id)));
					retweetStatus.setText(cursor.getString(cursor.getColumnIndex(retweet_status_text)));
					retweetStatus.setThumbnail_pic(cursor.getString(cursor.getColumnIndex(retweet_status_pic)));
					retweetStatus.setReposts_count(cursor.getInt(cursor.getColumnIndex(retweet_reposts_count)));
					retweetStatus.setComments_count(cursor.getInt(cursor.getColumnIndex(retweet_comments_count)));
					retweetStatus.setSource(cursor.getString(cursor.getColumnIndex(retweet_source)));
					retweetStatus.setCreated_at(cursor.getString(cursor.getColumnIndex(retweet_created_at)));
					retweetStatus.setOriginal_pic(cursor.getString(cursor.getColumnIndex(retweet_original_pic)));
					retweetStatus.setBmiddle_pic(cursor.getString(cursor.getColumnIndex(retweet_bmiddle_pic)));
					UserEntry userEntry = new UserEntry();
					userEntry.setScreen_name(cursor.getString(cursor.getColumnIndex(retweet_screen_name)));
					retweetStatus.setUser(userEntry);
					entry.setRetweeted_status(retweetStatus);
				}
				entries.add(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entries;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
	}
}
